#include <stdio.h>
#include <sys/signal.h>
#include <sqlite3.h>
#include <string.h>
#include <sys/stat.h>
#include "sockcomADT.h"
#include "server.h"
#include "svstub.h"
#include "error.h"
#include "tickets.h"
#include "clsv.h"
#include "dbHandler/dbHandler.h"

/* List options definitions */
enum {
	LIST_CAT, LIST_USR
};

#define MAX_FILES    32

/* array with open files */
static FILE * openfiles[MAX_FILES];

/* verify if file is existing and open
static int
not_verify_rd(RD rd)
{
	if (rd < 0 || rd >= MAX_FILES)
		return -S_BAD_RD;
	if (openfiles[rd] == NULL)
		return -S_NOT_OPENED;
	return S_OK;
}
*/
 
long
getfilesize(const char * path)
{
	struct stat file_status;
	
	if (stat(path, &file_status) != 0)
	{
		//Error(ERROR_FILESTATUS);
	}
	return file_status.st_size;
}

int
passwd(char * oldpass, char * newpass, int login)
{
	if (checkPass("proto.db", login, oldpass))
		return changePass("proto.db", login, newpass);
	else
		return 0;
}

int
list_categories(int offset, void * data)
{
	char * list;
	char * test = data;
	int qty = MAX_LIST;
	
	requestCatList("proto.db", &list);
	
	if ((offset + qty) > strlen(list))
		qty = strlen(list) - offset;
	
	memcpy(data, (void *)(list+offset), qty);
	((char *)data)[qty] = '\0';
	
	free(list);
	return qty;
}

int
list_users(int offset, void * data)
{
	char * list;
	char * test = data;
	int qty = MAX_LIST;
	
	requestUsrList("proto.db", &list);
	
	if ((offset + qty) > strlen(list))
		qty = strlen(list) - offset;
	
	memcpy(data, (void *)(list+offset), qty);
	test[qty] = '\0';
	
	free(list);
	return qty;
}

int
list_files(int offset, char * category, void * data)
{
	char * list;
	char * test = data;
	int qty = MAX_LIST;
	
	requestFList("proto.db", category, &list);
	
	if ((offset + qty) > strlen(list))
		qty = strlen(list) - offset;
	
	memcpy(data, (void *)(list+offset), qty);
	test[qty] = '\0';
	printf("%s\n", test);
	
	free(list);
	return qty;
}

int
get_ticket(char * filename, ticketP * tickp, int login)
{
	int pid, err;
	char * ip;
	
	//TODO: arreglar fileExists para que chequee en la base
	if (!fileExists(filename))
		return -ERROR_FILE_NOT_EXISTS;
	
	if (TicketsLeft(_DB_PATH_, login))		
	{
		pid = getpid();
		ip = printIP(comm);
		if (CreateTicket(_DB_PATH_, filename, pid, tickp, ip) == -1)
			return -ERROR_TICKET_CREATION;
		if ((err = IncTickets(_DB_PATH_, login)) == -1 || err == 0)				
			return -ERROR_TICKET_CREATION;
		
		return 1;
	}
	else
		return -ERROR_NO_TICKETS_LEFT;
}

int
use_ticket(ticketT md5, ticketP * tickp, int login)
{	
	int status;
	int ctime;
	char * ip;
	
	if (!login)
		return -ERROR_NOT_LOGGED_IN;
	
	ip = printIP(comm);
	if (!TicketExists(_DB_PATH_, md5, ip))
	{
		strcpy(tickp->id, md5);
		return -TICKET_NOT_EXIST;
	}
	
	if ((status = GetTicketParameters(_DB_PATH_, md5, tickp))== -1)
		return status;
	
	ctime = time(NULL);

	if (tickp->etime > ctime && tickp->dtime <= ctime)
	{	/*ticket not enabled*/
		if (tickp->retries == 2)
		{
			if (RemoveTicket(_DB_PATH_, md5) == -1)
				return -ERROR_TICKET;
			return -TICKET_LOST;
		}
		else
		{
			if (IncreaseRetries(_DB_PATH_, md5, tickp) == -1)
				return -ERROR_TICKET;
			return -NOT_ENABLED;
		}
	}
	else
	{
		if (tickp->etime <= ctime && tickp->ftime >= ctime)
		{
			if (RemoveTicket(_DB_PATH_, md5) == -1)
				return -ERROR_TICKET;
			return 1;
		}
		else
		{
			if (RemoveTicket(_DB_PATH_, md5) == -1)
				return -ERROR_TICKET;
			return -TICKET_EXPIRED;
		}
	}
}
